﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ElectronicsMarket.Service.IService;
using ElectronicsMarket.Service.Models;
using ElectronicsMarket.Service;
using ElectronicsMarket_Desktop.BL;
namespace ElectronicsMarket_Desktop
{
    public partial class frmThongKe : Form
    {
        public frmThongKe()
        {
            InitializeComponent();
        }
        ElectronicsMarketDBClassesDataContext eMEntity;
        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void lblDenNgay_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void frmThongKe_Load(object sender, EventArgs e)
        {
            dataBind();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            getProductByDay(Convert.ToInt32 (cbosanpham.SelectedValue), dtTuNgay.Value, dtDenNgay.Value);
            
        }
        public void getProductByDay(int id, DateTime tungay, DateTime denngay)
        {
            eMEntity = new ElectronicsMarketDBClassesDataContext(StaticParameter.Connect);
            var resuilt = from p in eMEntity.PRODUCTs
                          from o in eMEntity.ORDERs
                          from od in eMEntity.ORDERS_DETAILs
                          where p.PRODUCT_ID == id && p.PRODUCT_ID == od.PRODUCT_ID && o.ORDERS_ID == od.ORDERS_ID && o.ORDERS_CREATED_AT >= tungay && o.ORDERS_CREATED_AT <= denngay
                          select new
                          {
                              p.PRODUCT_NAME,
                              o.ORDERS_CREATED_AT,
                              od.AMOUNT,
                          };
            grdSanPhamThongKe.AutoGenerateColumns = false;
            grdSanPhamThongKe.DataSource = resuilt;
            txtsoluong.Text = tongsoluong().ToString();
          
        }
        public void dataBind()
        {
            ProductBL productbl = new ProductBL();
            cbosanpham.DataSource = productbl.getAllProduct();
            cbosanpham.DisplayMember = "PRODUCT_NAME";
            cbosanpham.ValueMember = "PRODUCT_ID";
        }
        public int tongsoluong()
        {
            int tong = 0;
            for (int i = 0; i < grdSanPhamThongKe.Rows.Count; i++)
            {
                tong = tong + Convert.ToInt32(grdSanPhamThongKe.Rows[i].Cells["colsoluong"].Value);
            }
            return tong;

        }

    }
}
